package com.aistar.controller;

import com.aistar.pojo.Product;
import com.aistar.pojo.ProductImages;
import com.aistar.pojo.Seckill;
import com.aistar.service.ProductImagesService;
import com.aistar.service.ProductService;
import com.aistar.service.SecKillService;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductImagesService productImagesService;
    @Autowired
    private SecKillService secKillService;


    @RequestMapping("/product/detail")
    public String productDetail() {
        return "product/detail";
    }

    @RequestMapping("/product/confirmorder")
    public String confirmorder() {
        return "product/confirmOrder";
    }


    @RequestMapping("/product/{id}")
    @ResponseBody
    public ServerResponse getById(@PathVariable("id") Integer id) {
        return productService.getById(id);
    }

    /*热门推荐
     *
     * */
    @RequestMapping("/product/gethotproduct")
    @ResponseBody
    public ServerResponse getHotProduct() {
        ServerResponse serverResponse = productService.getHotProduct();
        return serverResponse;
    }

    @RequestMapping("/product/getrimproduct")
    @ResponseBody
    public ServerResponse getRimProduct(HttpServletRequest request) {
        String site = request.getParameter("site");
        ServerResponse serverResponse = productService.getRimProduct(site);
        return serverResponse;
    }

    @RequestMapping("/product/getlongproduct")
    @ResponseBody
    public ServerResponse getLongProduct() {
        ServerResponse serverResponse = productService.getLongProduct();
        return serverResponse;
    }

    /*热门推荐
     *
     * */
    @RequestMapping("/product/search")
    public ModelAndView searchProduct(String kw) {
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("kw", kw);
        modelAndView.setViewName("product/all_search");
        return modelAndView;
    }

    @RequestMapping("/product/getOne/{id}")
    public String getOne(@PathVariable("id") Integer id, HttpSession session) {
        ServerResponse serverResponse = productService.getById(id);
        Product product = (Product) serverResponse.getData();
        ServerResponse serverResponse1 = productImagesService.getByProId(product.getId());
        List<ProductImages> productImagesList = (List<ProductImages>) serverResponse1.getData();

        session.setAttribute("product", product);
        session.setAttribute("productImagesList", productImagesList);
        session.setAttribute("seckill", null);
        return "product/detail";

    }




    @RequestMapping("/product/all_search")
    @ResponseBody
    public ServerResponse getAllSearch(Integer pageNum, String keyword, String method,
                                       String days, String startCity, String feature,
                                       String minPrice, String maxPrice, Integer sortStatus) {
//        多条件搜索条件：pageNum=1
//        多条件搜索条件：keyword=三亚
//        多条件搜索条件：method=跟团游
//        多条件搜索条件：days=4
//        多条件搜索条件：startCity=上海
//        多条件搜索条件：feature=日光沙滩
//        多条件搜索条件：minPrice=4001
//        多条件搜索条件：maxPrice=5000
        //====================参数数据类型处理===================
        Integer day;
        Float minPrices;
        Float maxPrices;

        if (keyword == null || keyword.trim().equals(""))
            keyword = null;
        if (method == null || method.trim().equals(""))
            method = null;
        if (startCity == null || startCity.trim().equals(""))
            startCity = null;
        if (feature == null || feature.trim().equals(""))
            feature = null;
        if (days == null || days.trim().equals(""))
            day = null;
        else
            day = Integer.valueOf(days);
        if (minPrice == null || minPrice.trim().equals(""))
            minPrices = null;
        else
            minPrices = Float.valueOf(minPrice);
        if (maxPrice == null || maxPrice.trim().equals(""))
            maxPrices = null;
        else
            maxPrices = Float.valueOf(maxPrice);
        if (pageNum == null)
            pageNum = 1;


        //3. ====================service==================
        return productService.getAllSearch(pageNum, keyword, method,
                day, startCity, feature,
                minPrices, maxPrices, sortStatus);
    }


    /*   @RequestMapping("/product/detail")
       public String detail(){
           return "product/detail";

       }*/
    /*==================获得秒杀列表========================*/
    @GetMapping("/product/getseckill")
    @ResponseBody
    public ServerResponse getSecKillList() {

        ServerResponse serverResponse = productService.getSecKillList();
        return serverResponse;
    }
    //秒杀跳转至详情页准备抢购
    @RequestMapping("/product/seckill/{seckillId}")
    public String recKill(@PathVariable("seckillId") Integer seckillId, HttpSession session) {
        ServerResponse secKillResponse = secKillService.getById(seckillId);
        Seckill seckill = (Seckill) secKillResponse.getData();
        ServerResponse serverResponse = productService.getById(seckill.getProductId());
        Product product = (Product) serverResponse.getData();
        ServerResponse serverResponse1 = productImagesService.getByProId(product.getId());
        List<ProductImages> productImagesList = (List<ProductImages>) serverResponse1.getData();
        product.setPersonPrice((float) (product.getPersonPrice() * seckill.getSeckillRebate()));
        session.setAttribute("product", product);
        session.setAttribute("productImagesList", productImagesList);
        session.setAttribute("seckill", seckill);
        return "product/detail";

    }
}

